import { _decorator, Component, Node, RigidBody, Animation, Collider2D, Physics2DManifoldType, ERigidBodyType, Contact2DType, Vec2, Vec3, Enum } from 'cc';
import { Constant } from '../AshFramework/Datas/Constant';
import { Monster } from './Monster';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { Terminus } from './Terminus';
import { GameManager } from './GameManager';
import { WeaponManager } from './WeaponManager';

const { ccclass, property } = _decorator;
export enum WEAPONTYPE {
    WEAPON_1,
    WEAPON_2,
    WEAPON_3,
}

@ccclass('Weapon')
export class Weapon extends Component {
    @property
    CD: number = 2;

    @property
    Harm: number = 2;

    @property({ type: Enum(WEAPONTYPE) })
    WeaponType: WEAPONTYPE = WEAPONTYPE.WEAPON_1;

    target: Node = null;
    pos: Vec3 = Vec3.ZERO;



    private _rigidBoy: RigidBody = null;
    private _weapon: Node = null;
    private _animation: Animation = null;
    // private _arrMonster: Array<Node> = new Array();
    private _timer: number = 0;
    private _cb: Function = null;

    private _terminus: Node = null;

    protected onLoad(): void {
        this._rigidBoy = this.getComponent(RigidBody);
        this._weapon = this.node.getChildByName("武器");
        this._animation = this._weapon.getComponent(Animation);

        this.Harm = Math.floor((1 + GameManager.Instance.userData.propertyAmplification.WEAPON_HARM / 100) * this.Harm);//属性赋值

        const collider: Collider2D = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }

        this._animation.on(Animation.EventType.FINISHED, this.onAnimationFinished, this);
    }

    onBeginContact(self: Collider2D, other: Collider2D) {
        if (other.group === Constant.Group.monster && other.tag == 0) {
            if (!this.target) {
                this.target = other.node;
            }
            // this._arrMonster.push(other.node);
            WeaponManager.Instance.addMonster(this.WeaponType, other.node);
        }
        if (other.group === Constant.Group.terminus && other.tag == 0 && !this._terminus) {
            this._terminus = other.node;
        }
    }

    protected update(dt: number): void {
        this._timer += dt;
        this.node.position = this.pos;

        if (this.getTarget() && this._timer >= this.CD) {
            this._timer = 0;
            this.attack();
        }

        if (this.target) {

        }

    }

    attack() {
        this.target = this.getTarget();
        WeaponManager.Instance.logMap();
    }

    attackOne() {
        if (this.target === this._terminus) {
            this.target.getComponent(Terminus).beHit(this.Harm);
        } else {
            this.target.getComponent(Monster).beHit(this.Harm);//单体伤害
        }
    }

    attackAll() {
        if (this._terminus) {
            console.log(this._terminus);

            this._terminus.getComponent(Terminus).beHit(this.Harm);
        }
        let arrMonster = WeaponManager.Instance.getMonsters(this.WeaponType);
        for (let i = 0; i < arrMonster.length; i++) {
            if (arrMonster[i] && arrMonster[i].isValid) {
                arrMonster[i].getComponent(Monster).beHit(this.Harm);
            }
        }
    }

    getRotateToTarget() {
        // 获取节点的位置
        if (!this.target) return 0;
        const targetPos = this.target.getWorldPosition().clone();
        const weaponPos = this.node.getWorldPosition().clone();
        // 计算方向向量
        const direction = new Vec2(targetPos.x - weaponPos.x, targetPos.y - weaponPos.y);
        // 计算角度
        const angleRadians = Math.atan2(direction.y, direction.x);
        // 将弧度转换为度数
        const angleDegrees = angleRadians * (180 / Math.PI);

        // 设置节点的旋转角度
        return angleDegrees;
    }

    playAttackAni(cb: Function = null) {
        this._animation.stop();
        this._animation.play();
        this._cb = cb;
    }

    onAnimationFinished() {
        this._cb && this._cb();
        this._cb = null;
    }

    // removeMonster(monster: Node) {
    //     if (!monster || !monster.isValid) return;
    //     for (let i = 0; i < this._arrMonster.length; i++) {
    //         if (this._arrMonster[i] === monster) {
    //             this._arrMonster.splice(i, 1);
    //             return;
    //         }
    //     }
    //     if (this.target === monster) {
    //         this.target = this._arrMonster[0] ? this._arrMonster[0] : null;
    //     }
    // }

    // isExist(monster: Node): boolean {
    //     if (!monster) return false;
    //     return this._arrMonster.find(e => e === monster) ? true : false;
    // }

    getTarget(): Node | null {
        if (this.target && this.target.isValid) return this.target;
        if (this._terminus) return this._terminus;

        let arrMonster = WeaponManager.Instance.getMonsters(this.WeaponType);
        for (let i = 0; i < arrMonster.length; i++) {
            if (arrMonster[i].isValid) {
                return arrMonster[i];
            }
        }
        return null;
    }

    // protected onEnable(): void {
    //     EventManager.on(MyEvent.REMOVE_MONSTER, this.removeMonster, this);
    // }

    // protected onDisable(): void {
    //     EventManager.off(MyEvent.REMOVE_MONSTER, this.removeMonster, this);
    // }

}


